﻿using System;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using FBProject.BusinessLogic;
using FBProject.BusinessLogic.MSProjectProxies;

namespace FBProject
{
    static class Program
    {
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(Program));

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1810:InitializeReferenceTypeStaticFieldsInline")]
        static Program()
        {
            log4net.Config.XmlConfigurator.Configure();
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //FTaskDifferencesTest();
            //return;

            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Logger.InfoFormat(new string('=', 70));
            Logger.InfoFormat("Starting application {0}", Application.ProductName);
            Logger.InfoFormat("AssemblyFullName: {0}", Assembly.GetExecutingAssembly().GetName().FullName);
            Logger.DebugFormat("CurrentCulture:   {0}", Application.CurrentCulture);
            Logger.DebugFormat("StartupPath:      {0}", Application.StartupPath);
            Logger.DebugFormat("ExecutablePath:   {0}", Application.ExecutablePath);
            Logger.InfoFormat(new string('=', 70));

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (MSProjectApplicationProxy.IsMSProjectInstalled)
            {
                if (ApplicationConfiguration.AdvancedUI)
                {
                    using (var form = new FMain())
                    {
                        Application.Run(form);
                    }
                }
                else
                {
                    using (var form = new FWizard())
                    {
                        Application.Run(form);
                    }
                }
            }
            else
            {
                MessageBox.Show("Supported version of MS Project is not installed on this machine!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Logger.InfoFormat(new string('-', 70));
            Logger.InfoFormat("Stopping application {0}", Application.ProductName);
            Logger.InfoFormat(new string('-', 70));
        }

        public static void FTaskDifferencesTest()
        {
            var taskDifferences = new System.Collections.Generic.List<TaskDifference>();
            taskDifferences.Add(new TaskDifference(1, "Task 1", null, TaskDifferenceType.NewTask));
            taskDifferences.Add(new TaskDifference(2, "Task 2", "http://fb?2", TaskDifferenceType.DeletedTask));
            taskDifferences.Add(new TaskDifference(3, "Task 3", "http://fb?3", TaskDifferenceType.Milestone, "M1", "M2"));
            using (var target = new FTaskDifferences(taskDifferences))
            {
                Application.Run(target);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Logger.Fatal("Unhandled exception", e.ExceptionObject as Exception);
            }
            catch { }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            try
            {
                Logger.Fatal("Unhandled thread exception", e.Exception);
            }
            catch { }
        }
    }
}
